package com.wildwolf.project.wildwolf.ui.register;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.config.AppConfig;

public class BirthdayActivity extends Activity implements
		View.OnClickListener {
	
	private TextView prev = null;
	
	private TextView next = null;
	
	private DatePicker dp = null; 
	
	private TextView age = null; 
	
	private Intent intent =null;
	private boolean isLegalBirthday = false;
	private String birthday;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_reg_birthday);
		intent = getIntent();
		prev = (TextView) findViewById(R.id.textView_Birthday_Prev);
		next = (TextView) findViewById(R.id.textView_Birthday_Next);
		next.setEnabled(isLegalBirthday);
		dp = (DatePicker)findViewById(R.id.datePicker_brithday);
		age = (TextView)findViewById(R.id.textView_age);
		prev.setOnClickListener(this);
		next.setOnClickListener(this);
        final Calendar  calendar =Calendar.getInstance();
		dp.init(calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), new  DatePicker.OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				if(calendar.get(Calendar.YEAR)-year>0){
					isLegalBirthday = true;
					birthday= new  SimpleDateFormat(AppConfig.FORMAT_PATTERN).format(calendar.getTime());
				}
				next.setEnabled(isLegalBirthday);
				age.setText(String.valueOf(calendar.get(Calendar.YEAR)-year)+"岁");
			}
		});
		}

	@Override
	public void onClick(View v) {
		if (v == prev) {
			intent.setClass(this, IntroductionActivity.class);
			finish();
		} else if (v == next) {
			intent.setClass(this, AvatarActivity.class);
			intent.putExtra(AppConfig.REGISTERKEY.ENCOUNTER_REGISTER_BIRTHDAY, birthday);
		}
		startActivity(intent);
	}

}
